package com.shoppingmall.Adapter;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import com.example.shoppingmall.R;
import com.shopping.tools.FileUntils;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * 照片墙适配器
 */
public class PhotoWallAdapter extends BaseAdapter {
	// private ArrayList<PhotoWallRowModel> listData;
	private Context context;
	private MyHolder myHolder;
	private ArrayList<HashMap<String, String>> listData;
	/**
	 * 存放图片的HashMap 键：图片链接地址 值：图片
	 */
	private static HashMap<String, SoftReference<Drawable>> imgMap = new HashMap<String, SoftReference<Drawable>>();

	/**
	 * 下载图片的方法。判断imgMap中有无图片。有：直接放进ImageView中；无：先下载图片，然后放进ImageView中（
	 * 不过还要把图片资源放进imgMap中，方便下次使用）
	 */
	private void imgDown(ImageView view, int position,
			String url) {
		Drawable drawable;
		// 判断有无图片
		if ((imgMap.containsKey(url))
				&& (drawable = imgMap.get(url).get()) != null) {
			view.setImageDrawable(imgMap.get(url).get());
		} else {
			ImgDownTask img = new ImgDownTask(view, position);
			img.execute(url);
		}

	}

	class ImgDownTask extends AsyncTask<String, Integer, Drawable> {
		ImageView view;
		int position;
		String url;

		public ImgDownTask(ImageView view, int position) {
			this.view = view;
			this.position = position;
		}

		/**
		 * 执行耗时的操作-------------下载图片
		 */
		protected Drawable doInBackground(String... params) {
			url = params[0];
			return FileUntils.getFileUntils().getImageDownload(url);
		}

		/**
		 * 耗时操作后所要执行的--------------加载图片
		 */
		protected void onPostExecute(Drawable result) {
			view.setImageDrawable(result);
			imgMap.put(url, new SoftReference<Drawable>(result));
			super.onPostExecute(result);
		}

	}

	public PhotoWallAdapter(Context context,
			ArrayList<HashMap<String, String>> listData) {
		this.context = context;
		this.listData = listData;
	}

	public int getCount() {
		return listData.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listData.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		myHolder = null;
		if (convertView == null) {
			myHolder = new MyHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.photowall_gridview_item, null);
			myHolder.tv_praiseCount = (TextView) convertView
					.findViewById(R.id.tv_praiseCount);
			myHolder.tv_summary = (TextView) convertView
					.findViewById(R.id.tv_summary);
			myHolder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
			myHolder.img_photoThumbnail = (ImageView) convertView
					.findViewById(R.id.img_photoThumbnail);
			convertView.setTag(myHolder);// 把myholder对象放到convertView中
		} else {
			myHolder = (MyHolder) convertView.getTag();
		}
		String url = listData.get(position).get("photoThumbnail");// 图片的路径
		imgDown(myHolder.img_photoThumbnail, position, url);
		myHolder.img_photoThumbnail.setPadding(6,6,6,0);
		myHolder.tv_praiseCount.setText(listData.get(position).get(
				"praiseCount"));
		myHolder.tv_summary.setText(listData.get(position).get("summary"));

		return convertView;
	}

	final class MyHolder {
		TextView tv_praiseCount, tv_summary, tv_id;
		ImageView img_photoThumbnail, img_praise;

	}

}
